package server;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

import GUI.GuiServer;
import action.*;


public class Server {
	
	
	
	public Server(){
	
	}

	
	public static String getHostAddress() {
		String ip="";
		Enumeration<NetworkInterface> nets;
		try {
			nets = NetworkInterface.getNetworkInterfaces();
			for (NetworkInterface netint : Collections.list(nets)){
					Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
					for (InetAddress inetAddress : Collections.list(inetAddresses)) {

						if(!inetAddress.isLoopbackAddress())
						ip += netint.getName() +" - "+ inetAddress.toString().replaceFirst("/","")+"\n";

					}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
	
		return ip;
	}
	
	
	
	public static void main(String args[]) throws IOException, AWTException{
		   DatagramSocket socket = new DatagramSocket(5000);
	       String myAddress = Server.getHostAddress();
	       GuiServer guiServer = new GuiServer();
	       guiServer.init(myAddress);
	       System.out.println("in attesa del client\n");
	       int screenWidth, screenHeigth;
	       screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
	       screenHeigth = Toolkit.getDefaultToolkit().getScreenSize().height;
	       Robot robot = new Robot();
	       Action a1 = new ActionLeftClickUp();
	       Action a2 = new ActionLeftClickDown();
	       Action a3 = new ActionRightClickUp();
	       Action a4 = new ActionRightClickDown();
	       Action a5 = new ActionMove(screenWidth,screenHeigth);
	       Action a6 = new ActionDoNothing();
	       Action a7 = new ActionConnect( socket,(ActionMove)a5);
	       a1.setRobot(robot);
	       a2.setRobot(robot);
	       a3.setRobot(robot);
	       a4.setRobot(robot);
	       a5.setRobot(robot);
	       

	       Parser parser = new Parser(a1, a2, a3, a4,a5,a6,a7);
	       ThreadServer threadServer = new ThreadServer(socket, parser);
	       threadServer.run();

		}
	
	

	 
}
